package com.rsa.controller;

import cn.hutool.core.bean.BeanUtil;
import com.rsa.annotations.RsaSecurityParameter;
import com.rsa.constant.Constant;
import com.rsa.domain.Persion;
import com.rsa.utils.RSAUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping
public class TestController {

    /**
     * 跳转rsa页面
     *
     * @return
     */

    @RequestMapping("/rsa")
    public String goRsa() {
        return "rsa";
    }

    /**
     * RSA测试
     *
     * @return object
     */
    @PostMapping("/rsaTest")
    @RsaSecurityParameter
    public Persion rsaTest(@RequestBody Persion info) {
        System.out.println(info.toString());
        String content = "内容";
        info.setName(content);
        return info;
    }

    static Object getData() {
        List<Persion> rows = new ArrayList<Persion>() {{
            add(new Persion() {{
                setAge(10);
                setName("詹三");
                setAddress("西安");
                setRemark("老师");
            }});
            add(new Persion() {{
                setAge(35);
                setName("李思思");
                setAddress("北京");
                setRemark("医生");
            }});
            add(new Persion() {{
                setAge(15);
                setName("李乐乐");
                setAddress("上海");
                setRemark("学生");
            }});
            add(new Persion() {{
                setAge(30);
                setName("王武");
                setAddress("广州");
                setRemark("老板");
            }});


        }};
        return rows;
    }

    @PostMapping("/page")
    @RsaSecurityParameter
    public Object page(@RequestBody Map<String, Object> map) {
        Integer pageSize = (Integer) map.get("pageSize");
        Integer pageNum = (Integer) map.get("pageNum");
        Object queryParams =  map.get("queryParams");
        Persion persion = BeanUtil.toBean(queryParams, Persion.class);
        System.out.println(" pageSize :" + pageSize);
        System.out.println(" pageNum :" + pageNum);
        System.out.println(" queryParams :" + persion.toString());
        Map page = new HashMap() {{
            put("total", 100);
            put("pageSize", 10);
            put("pageNum", 1);
            put("rows", getData());
        }};
        return page;
    }


    @GetMapping("/getById/{id}")
    @RsaSecurityParameter
    public Object getById(@PathVariable String id) {
        System.out.println(id);
        Persion info = new Persion() {{
            setAge(35);
            setName("李思思");
            setAddress("北京");
            setRemark("医生");
        }};
        return info;
    }

    @GetMapping("/list")
    @RsaSecurityParameter
    public Object list() {
        return getData();
    }


    public static void main(String[] args) {
        Map page = new HashMap() {{
            put("total", 100);
            put("pageSize", 10);
            put("pageNum", 1);
            put("rows", getData());
        }};
        // 加密
//        String encryptedData = RSAUtils.encryptedDataOnJava(page.toString(), Constant.PUBLIC_KEY);
        String encryptedData = "GnTKOqLyQsauIw7nTV3a9tzIGepXcGNY7b7XmG0ESsc7RMt0bsQGkd2NwSt/C1/Qi7eFBPJZNcM+DnHIYfOoci89ZSqG//hZrvZd0deHzjXM8UiQz4rCo8v6JmrHrcWa/8O0ie9pDJ3KXN4FDW8VroqXWb7UCbEc8RWLSW/dAi91m4DjYr8jG7m135HO6AA26dgQdimsj96fZ0/2DWronJ6iVaqbGZslzPuEi9xJRXGijyTzpHooItza3N4u4H6rPUmxVD9MJi0vMrdgxkYZK41eeSGOvZJjE4H/x5AQ2KotwVW4OXy0Svekryyf5gQiJeWI7i/LdxO7b1iRS+pAOYdRBaLBwe5dlWXmeLRvHn9qC3xNXJv90uj8REJwrNMByVIxJFIeIulhaZfD7SErhngpKmR2Y9t40X8UQCPfGmIKkR0f1bVQ3pn0fJXKAPP0ApKB7jA5NwqwGw+U7qijHpmNUGDp6go41LyeJ2yidMZiUKd4jfUnWJ5fzVUgZeIR";
        System.out.println(encryptedData);
        // 解密
        String decryptData = RSAUtils.decryptDataOnJava(encryptedData, Constant.PRIVATE_KEY);
        System.out.println(decryptData);
    }
}
